package com.lmpop.trade.ti_news;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.dao.IbatisDbmImpl;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.Config;
import com.lmpop.frame.util.FileIO;
import com.lmpop.frame.util.HtmlRegexpUtil;
import com.lmpop.trade.ts_module.Ts_moduleInfo;
import com.lmpop.updateHtml.updateHtml.UpdateArticleHtml;

public class Ti_newsInfo
  implements TradeInterf
{
  QueryList queryList;
  UpdateArticleHtml uah;
  FileIO io;
  Config conf;
  String rootpath;

  public Ti_newsInfo()
  {
    this.queryList = new QueryList();
    this.uah = new UpdateArticleHtml();
    this.io = new FileIO();
    this.conf = new Config();
    this.rootpath = this.conf.getString("rootpath");
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    HtmlRegexpUtil.reSetFilterRegexpArray();

    Hashtable newsMap = new Hashtable();
    newsMap.put("news_id", map.getString("news_id"));
    newsMap.put("cust_id", map.getString("cust_id"));
    newsMap.put("cat_attr", map.getString("cat_attr"));
    newsMap.put("state_code", map.getString("state_code"));
    newsMap.put("title", HtmlRegexpUtil.fiterHtmlTag(map.getString("title")));
    newsMap.put("content", HtmlRegexpUtil.fiterHtmlTag(map.getString("content")));
    newsMap.put("title_color", map.getString("title_color"));
    newsMap.put("content_sub", HtmlRegexpUtil.fiterHtmlTag(map.getString("content_sub")));
    newsMap.put("news_src", map.getString("news_src"));
    newsMap.put("news_key", map.getString("news_key"));
    newsMap.put("author", map.getString("author"));
    newsMap.put("editor", map.getString("editor"));
    newsMap.put("article_cat", map.getString("article_cat"));
    newsMap.put("is_comment", map.getString("is_comment"));
    newsMap.put("out_link", map.getString("out_link"));
    newsMap.put("click_num", "0");
    newsMap.put("user_id", map.getString("session_user_id"));
    newsMap.put("user_name", map.getString("session_user_name"));
    String result_code = "0";
    try {
      insert(dbmMgr, newsMap);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Hashtable newsMap)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_news", newsMap);
  }

  public SysMap create_alterPage(DbmInterf dbmMgr, SysMap map) {
    UpdateArticleHtml uah = new UpdateArticleHtml();
    uah.updateArticleByGoodsId("UR7Vm3ukN0Fw1r5", map.getString("news_id"), "0", "1");
    uah.updateArticleByGoodsId("UR7Vm3ukN0Fw1rl", map.getString("news_id"), "0", "1");
    uah.updateArticleByGoodsId("UR7Vm3ukN0Fw325", map.getString("news_id"), "0", "1");
    uah.updateArticleByGoodsId("UR7Vm3ukN0Fwrt5", map.getString("news_id"), "0", "1");
    return map;
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    Ts_moduleInfo moduleInfo = new Ts_moduleInfo();
    String modulepath = moduleInfo.getSavaPathByCode("news");
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        List list = null;
        String publish_date = "";
        for (int i = 0; i < pkStr.length; i++) {
          if ((pkStr[i] != null) && (!pkStr[i].equals(""))) {
            delete(dbmMgr, pkStr[i]);

            list = this.queryList.getList("getTi_newsByPk", pkStr[i]);
            if ((list == null) || (list.size() <= 0))
              continue;
            Hashtable newsmap = (Hashtable)list.get(0);
            if ((newsmap.get("in_date") == null) || (newsmap.get("in_date").toString().equals("")))
              continue;
            publish_date = newsmap.get("in_date").toString();
            publish_date = publish_date.substring(0, 10);
            FileIO.DelFile(this.rootpath + modulepath + publish_date + "/" + pkStr[i] + ".html");
          }
        }
      }

    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    map.setString("attach_root_id", pkid_string);
    map.setString("attach_sign", "delete");

    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_news where news_id=#value#
    dbmMgr.update("deleteTi_news", pkid);
    int x = checkNewsComment(pkid);
    if (x == 1)
    	//delete from ti_newscomment where news_id=#value#
      dbmMgr.update("deleteTi_newscommentinfo", pkid);
  }

  public int checkNewsComment(String news_id)
  {
    int g = 0;
    //select info_id from ti_newscomment where news_id= #news_id#
    List list = this.queryList.getList("checkNewsComment", news_id);
    if ((list != null) && (list.size() > 0))
      g = 1;
    return g;
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Hashtable newsMap = new Hashtable();
    newsMap.put("news_id", map.getString("news_id"));
    newsMap.put("cust_id", map.getString("cust_id"));
    newsMap.put("cat_attr", map.getString("cat_attr"));
    newsMap.put("state_code", map.getString("state_code"));
    newsMap.put("title", map.getString("title"));
    newsMap.put("content", map.getString("content"));
    newsMap.put("title_color", map.getString("title_color"));
    newsMap.put("content_sub", map.getString("content_sub"));
    newsMap.put("news_src", map.getString("news_src"));
    newsMap.put("news_key", map.getString("news_key"));
    newsMap.put("author", map.getString("author"));
    newsMap.put("editor", map.getString("editor"));
    newsMap.put("article_cat", map.getString("article_cat"));
    newsMap.put("is_comment", map.getString("is_comment"));
    newsMap.put("out_link", map.getString("out_link"));
    newsMap.put("click_num", map.getString("click_num"));
    newsMap.put("user_id", map.getString("user_id"));
    newsMap.put("user_name", map.getString("user_name"));
    String result_code = "0";
    try {
      update(dbmMgr, newsMap);
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Hashtable newsMap)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_news", newsMap);
  }

  public SysMap updateTi_newsRecommed(DbmInterf dbmMgr, SysMap map)
  {
    Ti_news ti_news = new Ti_news();
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            ti_news.setRecommend(map.getString("recommend"));
            ti_news.setNews_id(pkStr[i]);
            updateTi_newsRecommed(dbmMgr, ti_news);
          }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateTi_newsRecommed(DbmInterf dbmMgr, Ti_news ti_news)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_newsRecommed", ti_news);
  }

  public SysMap updateTi_newsIstop(DbmInterf dbmMgr, SysMap map)
  {
    Ti_news ti_news = new Ti_news();
    String pkid_string = map.getString("pkid");

    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            ti_news.setIs_top(map.getString("is_top"));
            ti_news.setNews_id(pkStr[i]);
            updateTi_newsIstop(dbmMgr, ti_news);
          }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateTi_newsIstop(DbmInterf dbmMgr, Ti_news ti_news)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_newsIstop", ti_news);
  }

  public SysMap updateTi_newsIshead(DbmInterf dbmMgr, SysMap map)
  {
    Ti_news ti_news = new Ti_news();
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            ti_news.setIs_head(map.getString("is_head"));
            ti_news.setNews_id(pkStr[i]);
            updateTi_newsIshead(dbmMgr, ti_news);
          }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateTi_newsIshead(DbmInterf dbmMgr, Ti_news ti_news)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_newsIshead", ti_news);
  }

  public SysMap updateTi_newsNewsState(DbmInterf dbmMgr, SysMap map)
  {
    Ti_news ti_news = new Ti_news();
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            Hashtable nMap = new Hashtable();
            nMap.put("state_code", map.getString("state_code"));
            nMap.put("news_id", pkStr[i]);
            updateTi_newsNewsState(dbmMgr, nMap);
          }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateTi_newsNewsState(DbmInterf dbmMgr, Hashtable nMap)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_newsNewsState", nMap);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Map ti_news = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_news.put("start", Integer.valueOf(start));
    ti_news.put("limit", Integer.valueOf(limit));

    return this.queryList.getList("getTi_newslist", ti_news);
  }

  public int getCountByObj(Object obj)
  {
    Map ti_news = (Hashtable)obj;
    List list = this.queryList.getList("selTi_newsSum", ti_news);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_newsByPk", pk);
  }

  public String getusername(String user_id)
  {
    String user_name = "";
    List namelist = this.queryList.getList("getusername", user_id);
    if ((namelist != null) && (namelist.size() > 0)) {
      Map map = (Hashtable)namelist.get(0);
      if (map.get("user_name") != null) {
        user_name = map.get("user_name").toString();
      }
    }
    return user_name;
  }

  public String getSelnewsByTLevel()
  {
    Ti_news ti_news = new Ti_news();
    String select = "";
    List list = this.queryList.getList("getSelnewsByTLevel", ti_news);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String id = ""; String text = "";
        if (map.get("news_id") != null) {
          id = map.get("news_id").toString();
        }
        if (map.get("title") != null) {
          text = map.get("title").toString();
        }
        select = select + "<option value=" + id + ">" + text + "</option>";
      }
    }
    return select;
  }

  public String gettitle(String news_id)
  {
    String title = "";
    List namelist = this.queryList.getList("gettitle", news_id);
    if ((namelist != null) && (namelist.size() > 0)) {
      Map map = (Hashtable)namelist.get(0);
      if (map.get("title") != null) {
        title = map.get("title").toString();
      }
    }
    return title;
  }

  public String getch_name(String ch_id)
  {
    String ch_name = "";
    List namelist = this.queryList.getList("getch_name", ch_id);
    if ((namelist != null) && (namelist.size() > 0)) {
      Map map = (Hashtable)namelist.get(0);
      if (map.get("ch_name") != null) {
        ch_name = map.get("ch_name").toString();
      }
    }
    return ch_name;
  }

  public String getNewsSelectAll(String checkId)
  {
    List bList = this.queryList.getList("getNewsSelectAll", "");
    StringBuffer select = new StringBuffer();
    if ((null != bList) && (bList.size() > 0)) {
      for (int i = 0; i < bList.size(); i++) {
        String info_id = ""; String title = "";
        Hashtable bMap = (Hashtable)bList.get(i);
        if (bMap.get("news_id") != null)
        {
          info_id = bMap.get("news_id").toString();
        }
        if (bMap.get("title") != null)
        {
          title = bMap.get("title").toString();
        }
        if (checkId.equals(info_id))
          select.append("<option value='" + info_id + "' selected>" + title + "</option>");
        else {
          select.append("<option value='" + info_id + "'>" + title + "</option>");
        }
      }
    }
    return select.toString();
  }

  public String getCustSelectAll(String checkId)
  {
    List bList = this.queryList.getList("getCustSelectAll", "");
    StringBuffer select = new StringBuffer();
    if ((null != bList) && (bList.size() > 0)) {
      for (int i = 0; i < bList.size(); i++) {
        String info_id = ""; String title = "";
        Hashtable bMap = (Hashtable)bList.get(i);
        if (bMap.get("cust_id") != null)
        {
          info_id = bMap.get("cust_id").toString();
        }
        if (bMap.get("cust_name") != null)
        {
          title = bMap.get("cust_name").toString();
        }
        if (checkId.equals(info_id))
          select.append("<option value='" + info_id + "' selected>" + title + "</option>");
        else {
          select.append("<option value='" + info_id + "'>" + title + "</option>");
        }
      }
    }
    return select.toString();
  }

  public String getNewsToSelect(String type, String news_id, String title)
  {
    String selectItems = "<select name=\"" + type + "_select1\" id=\"" + type + "_select1\" size=\"10\" style=\"width:90%\" multiple=\"true\">";
    Ti_news ti_news = new Ti_news();
    if (!news_id.equals("")) ti_news.setNews_id(news_id);
    if (!title.equals("")) ti_news.setTitle(title);

    List list = this.queryList.getList("getNewsToSelect", ti_news);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String newsId = ""; String s_title = "";
        if (map.get("news_id") != null) {
          newsId = map.get("news_id").toString();
        }
        if (map.get("title") != null) {
          s_title = map.get("title").toString();
        }
        selectItems = selectItems + "<option value='" + newsId + "'>" + s_title + "</option>";
      }
    }

    selectItems = selectItems + "</select>";
    return selectItems;
  }

  public int checkNews(String news_id)
  {
    int g = 0;
    List list = this.queryList.getList("checkNews", news_id);
    if ((list != null) && (list.size() > 0))
      g = 1;
    return g;
  }

  public List getnewsListForTable(String news_id, String title)
  {
    Ti_news ti_news = new Ti_news();
    if (!news_id.equals("")) ti_news.setNews_id(news_id);
    if (!title.equals("")) ti_news.setTitle(title);
    return this.queryList.getList("getnewsListForTable", ti_news);
  }

  public List getDWMListByPage(int limit, int method)
  {
    Map params = new Hashtable();
    String sqlID = "getTi_newsMonthlist";
    params.put("limit", Integer.valueOf(limit));
    if (method == 1)
      sqlID = "getTi_newsDaylist";
    else if (method == 2)
      sqlID = "getTi_newsWeeklist";
    else if (method == 3) {
      sqlID = "getTi_newsMonthlist";
    }
    return this.queryList.getList(sqlID, params);
  }

  public void updateNewsClickNum(String info_id)
  {
    Map params = new Hashtable();
    params.put("news_id", info_id);
    DbmInterf dbmMgr = new IbatisDbmImpl();
    dbmMgr.updateSql("updateNewsClickNum", params);
  }

  public String getNewsClickNum(String news_id)
  {
    Map params = new Hashtable();
    params.put("news_id", news_id);
    String click_num = "";
    List namelist = this.queryList.getList("getNewsClickNum", params);
    if ((namelist != null) && (namelist.size() > 0)) {
      Map map = (Hashtable)namelist.get(0);
      if (map.get("click_num") != null)
        click_num = map.get("click_num").toString();
    }
    return click_num;
  }
}